package ru.ifmo.swing.genetics.automaton;

import org.uncommons.watchmaker.swing.NumericParameterControl;
import ru.ifmo.automaton.Automaton;
import ru.ifmo.automaton.generators.fixed.StatesCountDependedObject;
import ru.ifmo.common.Source;
import ru.ifmo.swing.EvolutionControlWithConfigProvider;
import ru.ifmo.swing.Panel;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author avhaliullin
 */
public class StatesCountDependedAutomatonControl<C, S> implements Source<Automaton<C, S>>, EvolutionControlWithConfigProvider {
    private final NumericParameterControl<Integer> statesCountControl;
    private final Panel control;
    private final Source<Automaton<C, S>> automatonSource;

    public StatesCountDependedAutomatonControl(int minStates, int maxStates, int defaultStates, int step, Source<Automaton<C, S>> automatonSource) {
        this.automatonSource = automatonSource;
        control = new Panel();
        control.setHorizontal(false);

        if (automatonSource instanceof StatesCountDependedObject) {
            statesCountControl = new NumericParameterControl<Integer>(minStates, maxStates, step, defaultStates);
            statesCountControl.getControl().setMaximumSize(statesCountControl.getControl().getMinimumSize());
            ((StatesCountDependedObject) automatonSource).setStatesCount(statesCountControl.getNumberGenerator());

            control.setTitle("States count: ");
            control.add(statesCountControl.getControl());
        } else {
            statesCountControl = null;
        }
    }

    public JComponent getControl() {
        return control;
    }

    public void reset() {
        if (statesCountControl != null) {
            statesCountControl.reset();
        }
    }

    public void setDescription(String description) {
        control.setTitle(description);
    }

    public Automaton<C, S> create() {
        return automatonSource.create();
    }

    public Map<String, Object> getParamValues() {
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("States count", statesCountControl.getNumberGenerator().nextValue());
        return res;
    }
}
